using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;



namespace SkinableFormsLib
{
	public class LWLinkLabel : LinkLabel, ILWControl
	{

		internal Bitmap _lwBitmap = null;
		internal Rectangle _lwRectangle;
		internal byte rc;
		internal byte gc;
		internal byte bc;

		internal Color _lwDesiredBackColor = RichTextBox.DefaultBackColor;
		internal bool _lwTransparency = false;
		internal bool _lwTransparent = true;
		internal Color _lwTransparentColor = Color.FromArgb(255, 3, 3, 3);

		public LWLinkLabel()
			: base()
		{
			_lwDesiredBackColor = base.BackColor;
		}


		internal bool _lwDragForm = false;
		public bool LWDragForm
		{
			get
			{
				return _lwDragForm;
			}
			set
			{
				_lwDragForm = value;
			}
		}





		/// <summary>
		/// Recomputes the internal Bitmap size
		/// </summary>
		/// <param name="e"></param>
		protected override void OnResize(System.EventArgs e)
		{
			if (_lwBitmap == null ||
				_lwBitmap.Width != Width || _lwBitmap.Height != Height)
			{

				if (Width != 0 && Height != 0)
				{
					DisposeInternal();
					_lwBitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
					_lwRectangle = new Rectangle(0, 0, _lwBitmap.Width, _lwBitmap.Height);
				}
			}
		}


		protected override void WndProc(ref Message m)
		{
			base.WndProc(ref m);


			//if (m.Msg == Win32.WM_ERASEBKGND)//optional - needs more testing
			if (m.Msg == Win32.WM_PAINT)
			{
				if (!DesignMode)
				{
					if (_lwBitmap == null)
					{
						OnResize(null);
					}
					#region comment
					/*if (Win32.SmoothingModeIsClearType())
					{
						ret = false;
						if (BackColor != _lwDesiredBackColor)
							BackColor = _lwDesiredBackColor;

					}
					else
					{
						if (_lwTransparent && !ret)
						{
							ret = true;
							_lwDesiredBackColor = BackColor;
							BackColor = _lwTransparentColor;
						}
					}  */

					#endregion

					_lwTransparency = Utils.GetTransparency(this);
					this.DrawToBitmap(_lwBitmap, _lwRectangle);
					if (_lwTransparency)
					{
						Utils.BitmapMakeTransparent(ref _lwBitmap, ref _lwRectangle, ref  bc, ref gc, ref  rc);
						//or
						//Utils.BitmapMakeTransparentUnsafe(ref _lwBitmap, ref _lwRectangle, ref  bc, ref gc, ref  rc); //I suspect this one is faster

					}
				}
			}
		}




		private void DisposeInternal()
		{

			if (_lwBitmap != null)
				_lwBitmap.Dispose();

		}
















		#region ILWControl Members

		Bitmap ILWControl.LWBitmap
		{
			get
			{

				return _lwBitmap;
			}
		}

		public Color LWDesiredBackColor
		{
			get
			{
				return _lwDesiredBackColor;
			}
			set
			{
				_lwDesiredBackColor = value;
			}
		}

		public bool LWTransparent
		{
			get
			{
				return _lwTransparent;
			}
			set
			{
				_lwTransparent = value;
			}
		}

		public bool LWTransparency
		{
			get
			{
				return _lwTransparency;
			}
		}


		public Color LWTransparentColor
		{
			get
			{
				return _lwTransparentColor;
			}
			set
			{
				_lwTransparentColor = value;
				rc = _lwTransparentColor.R;
				gc = _lwTransparentColor.G;
				bc = _lwTransparentColor.B;
			}
		}


		#endregion
	}
}
